var websocket;

$(function(){
    /*
    * 请求 收件箱  数据   目前  只去  前5条
    * */
    var userid=$("#init").data("userid");
    $.post($(path).val()+'/Email/getEmailBox',{
        type:1,
        page:1,
        rows:3,
        sidx:"ctime",
        sord:"desc",
        userid:userid,
        readstatus:1,
    },
        function(data){
       // console.info(data)
        data=JSON.parse(data);
        if(data!=null&&data.dataList!=null) {
            var dataList = (data.dataList).reverse();
            $("#email_num").text(data.dataList.length)
            $.each(dataList,function(i,o){
                addMail_li(o);
            })
        }
    });

    WebSocketInit();

  //  alertmsg();


})

  /*
  * 给 usermail对象  页面上 生成一个信息框
  * */
function  addMail_li(o){
   // console.info(o)
    var sender =o.sender;
    var url="image/default.jpg";
    if(o.senduser!=null){
        if(o.senduser.url!=null&&o.senduser.url!="")
        url=o.senduser.url;
        if(sender==null)
        sender=o.senduser.realname;
    }
var li="<li class='m-t-xs' ><div class='dropdown-messages-box'>"+
            "<img alt='image' class='img-circle pull-left my_image' src='"+url+"'>"+
            "<div class='media-body'><small class='pull-right'>"+o.msg.timeAgo+"</small>"+
            "<strong id='sender'>"+sender+"  </strong>  "+(o.msg.content).substring(0,10)+"<br>"+
            "<small class='text-muted'>"+o.msg.ctimeToFullString+"</small></div></div> " +
    "<li class='divider'></li>" +
    "</li>";
        //插入 第一个的位置
    $("#email_ul").prepend(li);
}


function WebSocketInit(){
    if ('WebSocket' in window) {
        websocket = new WebSocket($("#ws").val()+"MsgServer");
    }
    else {
        websocket = new SockJS($("#basePath").val()+"sockjs/MsgServer");
    }
    websocket.onopen = function (evnt) {
        console.info('与msgsocket建立连接....');
    };
    websocket.onmessage = function (evnt) {
        var str=evnt.data;
        str=JSON.parse(str);
       // console.info(str);
        socketMsg(str);


    };
    websocket.onerror = function (evnt) {
        console.info('与msgsocket连接错误....');
    };
    websocket.onclose = function (evnt) {
        console.info('与msgsocket的连接关闭');
    }
    $("#sendMsg").click(function(){
       // websocket.send($("#textarea").val());
       // $("#textarea").val("");
    });

}


function socketMsg(o){
if(o!=null&&o.readstatus!=null&&o.msg!=null){
    alertEmailmsg(o);
}

}



/*
* 有邮件通知
* */
function alertEmailmsg(o){
    var url='image/default.jpg'
if(o.senduser!=null&&o.senduser.url!=null&&o.senduser.url!='')
    url=o.senduser.url;
    iziToast.show({
        class: 'test',
        color: 'dark',
        icon: 'icon-contacts',
        title: o.msg.title,
        message: (o.msg.content).substring(0,15),
        position: 'topRight',
        transitionIn: 'flipInX',
        transitionOut: 'flipOutX',
        progressBarColor: 'rgb(0, 255, 184)',
        image: url,
        imageWidth: 70,
        layout:2,
       /* onClose: function(){
            console.info('onClose');
        },*/

        iconColor: 'rgb(0, 255, 184)'
    });
    addMail_li(o);
    var num=parseInt($("#email_num").text())+1;
    $("#email_num").text($("#email_num").text()+"+"+1);
    setTimeout(function(){ $("#email_num").text(num);},10000);
    if(num>3){
        $($("#email_ul").children("li.m-t-xs")[3]).remove();
    }

};


